package Entities; //done

import Client.ConnectionToServer;

/**
 * This class controls Product Template  details and functions.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class Template {
	
	/** Template ID */
	private long TempID;
	
	/** Template Name */
	private String TempName;
	
	/** Client market */
	private String clientmarket;
	
	/** Message */
	private String msg;
	
	/** Product */
	private Product product;
	
	/**
	 * @param TempID
	 * @param TempName
	 * @param product
	 * @param clientmarket
	 * @param msg
	 */
	public Template(long TempID, String TempName, Product product, String clientmarket, String msg) {
		super();
		this.TempID = TempID;
		this.TempName = TempName;
		this.product = product;
		this.clientmarket = clientmarket;
		this.msg = msg;
	}
	
	
	/**
	 * @return the TempID
	 */
	public long getTemplateID() {
		return TempID;
	}


	/**
	 * @param TempID the TempID to set
	 */
	public void setTemplateID(long TempID) {
		this.TempID = TempID;
	}


	/**
	 * @return the TempName
	 */
	public String getTemplateName() {
		return TempName;
	}


	/**
	 * @param TempName the TempName to set
	 */
	public void setTemplateName(String TempName) {
		this.TempName = TempName;
	}


	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}


	/**
	 * @param product the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}


	/**
	 * @return the clientmarket
	 */
	public String getClientmarket() {
		return clientmarket;
	}


	/**
	 * @param clientmarket the clientmarket to set
	 */
	public void setClientmarket(String clientmarket) {
		this.clientmarket = clientmarket;
	}


	/**
	 * @return the message
	 */
	public String getMsg() {
		return msg;
	}


	/**
	 * @param msg the message to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}


	/**
	 * This function saves a new Template to DB
	 * @return String on success, else null
	 *
	 */
	public String saveTemplate() {
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.createTemplate(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function updates a Template in the DB
	 * @return String on success, null on fail
	 *
	 */
	public String updateTemplate() {
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.updateTemplate(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
	
	/**
	 * This function loads a Template from DB
	 * @return true on success , false on fail 
	 * @param TempID the id of campaign to load.
	 *
	 */
	public boolean loadTemplate(long TempID) {		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			String[] template = (String[])connectiontoserver.loadTemplate(TempID);
			if (template == null )				
				return false;
			if (template[4] == null)
				return false;//product does not exist
			setTemplateID(Long.parseLong(template[0]));//TempID
			setTemplateName(template[1]);//TempName
			setMsg(template[2]);//msg
			setClientmarket(template[3]);//TempSector			
			if (product == null)
				product = new Product();			
			getProduct().setPrice(Double.parseDouble(template[4]));//pPrice
			getProduct().setName(template[5]);//pName
			getProduct().setDescription(template[6]);//pDescription
			getProduct().setProdID(Long.parseLong(template[7]));//pID		
			return true;
		}
		catch(Exception e){
			return false;
		}				
	}
}
